import { Component, ViewChild, ViewChildren, AfterViewInit } from '@angular/core';
import { TreeNode } from 'primeng/primeng';
import { QkHumanSelectorComponent } from './qkhuman-selector.component';
import { QkUnitSelectorComponent } from './qkunit-selector.component';
import { QkRoleSelectorComponent } from './qkrole-selector.component';
import { QkPartService } from './qkpart.service';
import { DynamicDialogRef, DynamicDialogConfig } from 'primeng/api';

@Component({
  selector: 'qk-part',
  templateUrl: './qkpart.component.html',
  styleUrls: ['./qkpart.component.scss'],
  providers: [QkPartService]
})
export class QkPartSelectorComponent implements AfterViewInit {
  parts = new Array<TreeNode>();
  /**已选中节点 */
  checkedParts = new Array<TreeNode>();
  /**权限号，用于决定是否显示人员、岗位、机构 */
  authorityCode = 7;

  @ViewChildren(QkHumanSelectorComponent)
  private humanSelectorComponent: QkHumanSelectorComponent;
  @ViewChildren(QkUnitSelectorComponent)
  private unitSelectorComponent: QkUnitSelectorComponent;
  @ViewChildren(QkRoleSelectorComponent)
  private roleSelectorComponent: QkRoleSelectorComponent;

  getSelectedHuman() { }

  constructor(
    public ref: DynamicDialogRef,
    public config: DynamicDialogConfig
  ) {
    this.checkedParts = config.data.ckeckedParts;
    this.authorityCode = 1; //config.data.authorityCode;
  }

  ngAfterViewInit() {
  }

  /**选择确定时的操作 */
  public selectPart() {
    //人员
    if (this.humanSelectorComponent['length'] > 0) {
      let checkedHumans = this.humanSelectorComponent['first']['checkedHumans'];
      checkedHumans.forEach(node => {
        if (node['data']['type'] == 'H') this.parts.push(node);
      });
    }
    //机构
    if (this.unitSelectorComponent['length'] > 0) {
      let checkedUnits = this.unitSelectorComponent['first']['checkedUnits'];
      checkedUnits.forEach(node => {
        if (node['data']['type'] == 'U') this.parts.push(node);
      });
    }
    //岗位
    if (this.roleSelectorComponent['length'] > 0) {
      let checkedRoles = this.roleSelectorComponent['first']['checkedRoles'];
      checkedRoles.forEach(node => {
        if (node['data']['type'] == 'R') this.parts.push(node);
      });
    }
    this.ref.close(this.parts);
  }

  cancel(){
    this.ref.close();
  }
  /**根据栏目菜单进行鉴权 */
  checkAuth(code: number) {
    return (this.authorityCode & code) == code;
  }
}
